package com.yb.web.tribune;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yb.base.web.BaseAction;
import com.yb.bussiness.tribune.TribuneMasterService;
import com.yb.bussiness.tribune.TribuneMasterServiceImpl;
import com.yb.domain.member.TsSysuser;
import com.yb.domain.tribune.TribuneMaster;
import com.yb.util.comm.Contants;
import com.yb.util.comm.RequestUtil;
import com.yb.util.framework.PageUtil;

/**
 * TribuneMasterAction entity.
 * 
 * @author MyEclipse Persistence Tools
 */

public class TribuneMasterAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private TribuneMasterService service;
	private int total;
	private List rows;
	private TribuneMaster tMaster;
	
	public TribuneMaster getTMaster() {
		return tMaster;
	}

	public void setTMaster(TribuneMaster master) {
		tMaster = master;
	}

	public TribuneMasterAction() {
		service = new TribuneMasterServiceImpl();
	}

	/**
	 * 查询
	 * 
	 * @return
	 */
	public String searchTribuneMaster() {
		menuCls = RequestUtil.getParam(request, "menuCls", "");
		int page = RequestUtil.getParam(request, "page", 1);
		int row = RequestUtil.getParam(request, "rows", 10);
		String auding = RequestUtil.getParam(request, "auding", "");
		
		Map<String, String> map = paramMap();
		map.put("auding", auding);
		PageUtil pu = service.getList(map, page, row);
		total = pu.getTotalRecords();
		rows = pu.getList();
		return Contants.JSONSTRING_RESULT_NAME;
	}
	
	
	  public String loadpremind(){
	        PrintWriter out;
	        try{
	            response.reset();
	            response.setCharacterEncoding("utf-8");
	            out = response.getWriter();
	            Integer count = service.loadpremind();
	            out.write(count+"");
	        }catch (Exception e) {
	            log.error("加载新增的申请", e);
	        }
	        return null;
	    }
	
	
	
	/**
	 * 删除
	 * @return
	 */
	public String deleteTribuneMaster() {
		String idString = RequestUtil.getParam(request, "idString", "");

		boolean isFlag = false;
			isFlag = service.deleteByIdString(idString);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		if (isFlag)
			message = "删除成功";
		else
			message = "删除失败";
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");

		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {

			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 审核
	 * @return
	 */
	public String audingTribuneMaster() {
		String auding = RequestUtil.getParam(request, "auding", "");
		String idString = RequestUtil.getParam(request, "idString", "");
		
		boolean isFlag = false;
			isFlag = service.audingTribuneMaster(auding,idString);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		if (isFlag)
			message = "操作成功";
		else
			message = "操作失败";
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");
		
		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 申请成为版主
	 * @return
	 */
	public String toBeMaster() {
		TsSysuser tsSysuser = (TsSysuser) RequestUtil.getLoginUserFromSession(request, Contants.SESSION_USER);
		TribuneMaster obj = service.queryObjByUserId(tsSysuser.getId());
		if(null==obj){
			tMaster = new TribuneMaster();
			tMaster.setApplyTime(new Date());
			tMaster.setAuding("0");
			tMaster.setUserId(tsSysuser.getId());
			tMaster.setUserName(tsSysuser.getUsername());
			try {
				service.saveOrUpdate(tMaster);
				message = "您的申请已提交，请耐心等待";
			} catch (RuntimeException e1) {
				message = "提交失败";
				e1.printStackTrace();
			}
		}else if(!"1".equals(obj.getAuding())){
			obj.setApplyTime(new Date());
			obj.setAuding("0");
			try {
				service.saveOrUpdate(obj);
				message = "您的申请已提交，请耐心等待";
			} catch (RuntimeException e) {
				message = "提交失败";
				e.printStackTrace();
			}
		}else{
			message = "您的申请已提交，请耐心等待";
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");
		
		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	
	private Map<String,String> paramMap(){
		Map<String, String> map = new HashMap<String, String>();
		String name = RequestUtil.getParam(request, "name", "");
		String autding = RequestUtil.getParam(request, "autding", "");
		map.put("name", name);
		map.put("autding", autding);
		return map;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}

}